



#include "..\headers\BC_Core.h"




// initialize static members...
bool BCCore::windowClosing = false;


BCCore::BCCore(int winWidth , int winHeight , int winDepth , bool fullscreen):
																			mouseLState(false),
																			mouseRState(false),
																			wheelState(0),
																			mposx(0),
																			mposy(0),
																			mDeltaX(0),
																			mDeltaY(0),
																			window(NULL),
																			device(NULL),
																			d3d(NULL)
{
	// create a STUPID WINAPIwindow class...
	
	WNDCLASSEX wcex;
	ZeroMemory(&wcex,sizeof(WNDCLASSEX));
	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_DBLCLKS;
	wcex.lpfnWndProc	= BCWinCallback;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(0,IDC_ARROW);
	wcex.hInstance      = GetModuleHandle(0);
	wcex.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	wcex.lpszMenuName	= (LPCTSTR)0;
	wcex.lpszClassName	= "BATTLECITYWINDOW";
	wcex.hIconSm		= 0;

	bool condition = RegisterClassEx(&wcex);
	BC_CHECK_ERROR(condition , "Window Class Registering failed !")
	

	if (fullscreen)
	{
		window = CreateWindowEx(WS_EX_TOPMOST,"BATTLECITYWINDOW","BattleCity",WS_POPUP|WS_VISIBLE /*WS_OVERLAPPEDWINDOW|WS_VISIBLE*/ ,10,10,winWidth,winHeight,0,0,GetModuleHandle(0),0);
	}
	else
	{
		window = CreateWindowEx(0,"BATTLECITYWINDOW","BattleCity",WS_OVERLAPPEDWINDOW|WS_VISIBLE,10,10,winWidth,winHeight,0,0,GetModuleHandle(0),0);
	}

	BC_CHECK_ERROR(window , "Window Creation Failed !")

	// Create DX9 stofe :

	d3d = Direct3DCreate9(D3D_SDK_VERSION);

	BC_CHECK_ERROR(d3d,"Failed to create d3d9 interface !")


	D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = window;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferWidth = winWidth;
    d3dpp.BackBufferHeight = winHeight;

	// create the device :
	condition = SUCCEEDED(d3d->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      window,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &device));

    BC_CHECK_ERROR (condition , "Failed to Create DX9 Device !")
	

	// clear keyboard buffer...
	for (int x = 0; x <255; x++) keycodeBuffer[x] = false;
}



BCCore::~BCCore()
{
	device->Release();
	device = NULL;

	while (d3d->Release() > 0) {};
	d3d = NULL;
	 
	if (IsWindow(window)) DestroyWindow(window);
	UnregisterClass( "BATTLECITYWINDOW", GetModuleHandle(0) );
}






void BCCore::setWindowTitle(const std::string &title)
{
	SetWindowText(window,title.c_str());
}







bool BCCore::isKeyPressed(char keycode)
{
	return (GetKeyState(keycode) < 0 ? true : false);
}




bool BCCore::isLastPressed(char keycode)
{

	// Hehe...This part of code is sooo cool. Normaly we don't want diagonal movement in the game , We can move
	// ONLY in one direction at time . The direction is calculated from the last key we pressed...


	// if we hit the key , put it in the list of pressed keys :
	if (isKeyHit(keycode)) keyList.push_back(keycode);

	// if the key isn't pressed that means that probably it is released so remove the key from pressed keys list :
	if (!isKeyPressed(keycode)) keyList.remove(keycode);

	// if the list is empty , simply return false , as we have no keys pressed
	if (keyList.size() == 0) return false;

	// Finally compare our last added key to the key we want to check. If they are equal , that means the key we checking is the LAST pressed key
	if (keycode == keyList.back() ) return true;

	// all other conditions evaluates to false...
	return false;
}


bool BCCore::isKeyHit(char keycode)
{
	// if the key is in pressed state
	if (isKeyPressed(keycode))
	{
		//but it wasn't pressed earlier then return true as 
		// this is the first time we pressing it :
		if (!keycodeBuffer[keycode])
		{
			keycodeBuffer[keycode] = true;
			return true;
		}
	}
	else
	{
		keycodeBuffer[keycode] = false;
		return false;
	}

	return false;
}




BCVector2d BCCore::getMouseLocalPos()
{
	POINT pt;
	GetCursorPos(&pt);
	ScreenToClient(window , &pt);
	return BCVector2d(pt.x , pt.y);
}




BCVector2d BCCore::getMouseGlobalPos()
{
	POINT pt;
	GetCursorPos(&pt);
	return BCVector2d(pt.x , pt.y);
}








LRESULT BCCore::BCWinCallback(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

	if (msg == WM_CLOSE) 
	{
		windowClosing = true;
		return 0;
	}


	return DefWindowProc(hwnd,msg,wParam,lParam);
}





















